#!/bin/bash

##########################安装redis集群#######################
#3.1 安装依赖,如果yum安装很慢,可以挂载系统盘修改yum源后安装
gem install redis-3.0.0.gem

# 3.3 解压到执行目录后编译安装
tar zxvf redis-3.0.0.tar.gz
cd redis-3.0.0
make MALLOC=libc
make install PREFIX=/usr/local/redis

# 3.4 修改配置文件并设置守护运行
\cp redis.conf /usr/local/redis/bin/redis.conf
sed -i 's/daemonize no/daemonize yes/' /usr/local/redis/bin/redis.conf
sed -i 's/# cluster-enabled yes/cluster-enabled yes/' /usr/local/redis/bin/redis.conf
rm -rf /usr/local/redis/bin/dump.rdb
rm -rf /usr/local/redis/bin/appendonly.aof

# 3.5 创建副本,每台机器只开启两个redis实例(主备)
mkdir -p /usr/local/redis-cluster
cp -pr /usr/local/redis/bin /usr/local/redis-cluster/redis05
cp -pr /usr/local/redis/bin /usr/local/redis-cluster/redis06

# 3.6 杀死任何可能存在的redis进程
kill -9 $(ps -ef | grep redis | grep -v grep | awk '{print $2}')
kill -9 $(netstat -tlnp | grep 6379| awk '{print $7}' | awk -F '/' '{print $1}')

# 3.7 启动任何可能存在的redis实例
echo '#!/bin/bash
myPath=/usr/local/redis-cluster/redis01
if [ -d "$myPath" ]; then  
 cd "$myPath"
 sed -i "s/6379/7001/" /usr/local/redis-cluster/redis01/redis.conf
 /sbin/iptables -I INPUT -p tcp --dport 7001 -j ACCEPT
 /sbin/iptables -I INPUT -p tcp --dport 17001 -j ACCEPT
 ./redis-server /usr/local/redis-cluster/redis01/redis.conf
fi
myPath=/usr/local/redis-cluster/redis02
if [ -d "$myPath" ]; then  
 cd "$myPath"
 sed -i "s/6379/7002/" /usr/local/redis-cluster/redis02/redis.conf
 /sbin/iptables -I INPUT -p tcp --dport 7002 -j ACCEPT
 /sbin/iptables -I INPUT -p tcp --dport 17002 -j ACCEPT
 ./redis-server /usr/local/redis-cluster/redis02/redis.conf
fi
myPath=/usr/local/redis-cluster/redis03
if [ -d "$myPath" ]; then  
 cd "$myPath"
 sed -i "s/6379/7003/" /usr/local/redis-cluster/redis03/redis.conf
 /sbin/iptables -I INPUT -p tcp --dport 7003 -j ACCEPT
 /sbin/iptables -I INPUT -p tcp --dport 17003 -j ACCEPT
 ./redis-server /usr/local/redis-cluster/redis03/redis.conf
fi
myPath=/usr/local/redis-cluster/redis04
if [ -d "$myPath" ]; then  
 cd "$myPath"
 sed -i "s/6379/7004/" /usr/local/redis-cluster/redis04/redis.conf
 /sbin/iptables -I INPUT -p tcp --dport 7004 -j ACCEPT
 /sbin/iptables -I INPUT -p tcp --dport 17004 -j ACCEPT
 ./redis-server /usr/local/redis-cluster/redis04/redis.conf
fi
myPath=/usr/local/redis-cluster/redis05
if [ -d "$myPath" ]; then  
 cd "$myPath"
 sed -i "s/6379/7005/" /usr/local/redis-cluster/redis05/redis.conf
 /sbin/iptables -I INPUT -p tcp --dport 7005 -j ACCEPT
 /sbin/iptables -I INPUT -p tcp --dport 17005 -j ACCEPT
 ./redis-server /usr/local/redis-cluster/redis05/redis.conf
fi
myPath=/usr/local/redis-cluster/redis06
if [ -d "$myPath" ]; then  
 cd "$myPath"
 sed -i "s/6379/7006/" /usr/local/redis-cluster/redis06/redis.conf
 /sbin/iptables -I INPUT -p tcp --dport 7006 -j ACCEPT
 /sbin/iptables -I INPUT -p tcp --dport 17006 -j ACCEPT
 ./redis-server /usr/local/redis-cluster/redis06/redis.conf
fi
' > /usr/local/redis-cluster/redis-cluster.sh

chmod u+x /usr/local/redis-cluster/redis-cluster.sh

/usr/bin/sh /usr/local/redis-cluster/redis-cluster.sh

# 3.2 设置开机自启动
echo "
/usr/bin/sh /usr/local/redis-cluster/redis-cluster.sh
" >> /etc/rc.d/rc.local

chmod +x /etc/rc.d/rc.local


echo "finish"